package com.lsz.crm.controller;

import com.lsz.crm.base.BaseController;
import com.lsz.crm.base.ResultInfo;
import com.lsz.crm.model.TreeModel;
import com.lsz.crm.pojo.Module;
import com.lsz.crm.service.ModuleService;
import com.lsz.crm.utils.AssertUtil;
import org.apache.ibatis.annotations.ResultType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.jws.WebParam;
import java.util.List;
import java.util.Map;

/**
 * @Author liushizhong
 * @Date 2021/10/30 15:27
 * @Version 1.0
 */

@Controller
@RequestMapping("module")
public class ModuleController extends BaseController {

    @Autowired
    private ModuleService moduleService;

    /**
     * 资源管理页面
     * @return
     */
    @RequestMapping("index")
    public String index(){
        return "module/module";
    }

    /**
     * 查询对应角色的所有授权的资源
     * @return
     */
    @RequestMapping("queryAllModules")
    @ResponseBody
    public List<TreeModel> queryAllModules(Integer roleId) {
        return moduleService.queryAllModules(roleId);
    }

    /**
     * 转发到资源授权页面
     * @param roleId
     * @param model
     * @return
     */
    @RequestMapping("toAddGrantPage")
    public String toAddGrantPage(Integer roleId, Model model){
        AssertUtil.isTrue(roleId == null, "角色id不能为空!");
        model.addAttribute("roleId",roleId);
        return "role/grant";
    }

    /**
     * 查询资源列表
     * @return
     */
    @RequestMapping("list")
    @ResponseBody
    public Map<String,Object> queryModuleList(){
        return moduleService.queryModuleList();
    }

    /**
     * 添加资源
     * @param module
     * @return
     */
    @PostMapping("add")
    @ResponseBody
    public ResultInfo addModule(Module module){
        moduleService.addModule(module);
        return success("添加资源成功");
    }

    /**
     * 跳转到资源添加页面
     * @param grade
     * @param parentId
     * @return
     */
    @RequestMapping("toAddModule")
    public String toAddModule(Integer grade, Integer parentId,Model model){
        model.addAttribute("grade",grade);
        model.addAttribute("parentId",parentId);
        return "module/addModule";
    }

    /**
     * 更新资源⻚视图转发
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("toUpdateModule")
    public String toUpdateModule(Integer id,Model model){
        model.addAttribute("module",moduleService.selectByPrimaryKey(id));
        return "module/updateModule";
    }

    /**
     * 更新资源
     * @param module
     * @return
     */
    @PostMapping("update")
    @ResponseBody
    public ResultInfo update(Module module){
        moduleService.updateModule(module);
        return success("更新资源成功！");
    }

    /**
     * 删除资源
     * @param id
     * @return
     */
    @PostMapping("delete")
    @ResponseBody
    public ResultInfo deleteModule(Integer id){
        moduleService.deleteModule(id);
        return success("资源删除成功！");
    }
}
